package sg.edu.nus.iss.billing.service.subscription;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.common.exception.BadDaoCallException;
//import sg.edu.nus.iss.billing.dao.subscription.PlanDAO;
import sg.edu.nus.iss.billing.dao.subscription.SubscriptionDAO;
import sg.edu.nus.iss.billing.service.subscription.feature.CallTransferFeature;
import sg.edu.nus.iss.billing.service.subscription.feature.ChannelFeature;
import sg.edu.nus.iss.billing.service.subscription.feature.DataServiceFeature;
import sg.edu.nus.iss.billing.service.subscription.feature.Feature;
import sg.edu.nus.iss.billing.service.subscription.feature.IDDCallFeature;
import sg.edu.nus.iss.billing.service.subscription.feature.LocalCallFeature;
import sg.edu.nus.iss.billing.service.subscription.feature.PremiumChannelFeature;
import sg.edu.nus.iss.billing.service.subscription.feature.RoamingCallFeature;
import sg.edu.nus.iss.billing.service.subscription.feature.StandardChannelFeature;
import sg.edu.nus.iss.billing.vo.account.AccountVO;
import sg.edu.nus.iss.billing.vo.customer.CustomerVO;
//import sg.edu.nus.iss.billing.vo.plans.SubscriptionPlanVO;
import sg.edu.nus.iss.billing.vo.subscription.*;
import sg.edu.nus.iss.billing.vo.subscription.feature.CallTransferFeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.ChannelFeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.DataServiceFeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.FeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.IDDCallFeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.LocalCallFeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.PremiumChannelFeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.RoamingCallFeatureVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.StandardChannelFeatureVO;

public class SubscriptionImpl implements ISubscription {
	
	private static final String CLASSNAME = "ClassName SubscriptionImpl : ";
//	private ArrayList<SubscriptionVO> listSubscriptionVO;
	private CustomerVO customerVO;
	private CableTvVO cableTvVO;
	private DigitalVoiceVO digitalVoiceVO;
	private MobileVoiceVO mobileVoiceVO;
	private String accountNo;
	
	private boolean digitalVoiceExists =false;
	private boolean mobileVoiceExists =false;
	private boolean cableTvExists =false;
	
	ArrayList<SubscriptionVO>  planDetailsList = null;
	
	private HashMap<String, SubscriptionVO> hashPlanMap;
	
	
	private static String classname =  "Classname : SubscriptionImpl : ";
	
	public SubscriptionImpl(){
		//listSubscriptionVO = new ArrayList<SubscriptionVO>();
		try{
		planDetailsList = geAllPlans();
		hashPlanMap = new HashMap<String, SubscriptionVO>() ;
		setPlansAsMap(planDetailsList);
		}catch(Exception e){}
		
	}
	
	public SubscriptionImpl(String acctNo){
		//listSubscriptionVO = new ArrayList<SubscriptionVO>();
		this.accountNo = acctNo;
		try{
		planDetailsList = geAllPlans();
		hashPlanMap = new HashMap<String, SubscriptionVO>() ;
		setPlansAsMap(planDetailsList);
		}catch(Exception e){}
	}
	
	/* (non-Javadoc)
	 * @see sg.edu.nus.iss.billing.service.subscription.ISubscription#geAllPlans()
	 */
	@Override
	public ArrayList<SubscriptionVO>  geAllPlans(){
		SubscriptionDAO pd = new SubscriptionDAO();
		ArrayList<SubscriptionVO> listSubscriptionVO = (ArrayList<SubscriptionVO>) pd.getAllSubscriptionPlan();
		
		 return listSubscriptionVO;
	}
	
	private void setPlansAsMap(ArrayList<SubscriptionVO> listVO){
		
			if(listVO == null){
				listVO = geAllPlans();
			}
			 for (int i =0; i<listVO.size();i++){
				 
				 if(listVO.get(i) instanceof CableTvVO )	{
					 	hashPlanMap.put(Util.SUBSCRIPTIONVO_CABLETV,listVO.get(i));
					 	 System.out.println(classname + " CABLETV set " +hashPlanMap.size() );
				 }else if(listVO.get(i) instanceof MobileVoiceVO){
					 hashPlanMap.put(Util.SUBSCRIPTIONVO_MOBILEVOICE,listVO.get(i));
					 System.out.println(classname + " MobileVoiceVO set " +hashPlanMap.size() );
				 
				 }else if(listVO.get(i) instanceof DigitalVoiceVO){
					 hashPlanMap.put(Util.SUBSCRIPTIONVO_DIGITALVOICE,listVO.get(i));
					 System.out.println(classname + " DigitalVoiceVO set " +hashPlanMap.size() );
						 		
				 }
			 }
			 System.out.println(classname + " setPlansAsMap " +hashPlanMap.size() );
		
	}
	
	/* (non-Javadoc)
	 * @see sg.edu.nus.iss.billing.service.subscription.ISubscription#createSubscriptions(java.util.ArrayList)
	 */
	@Override
	public void   createSubscriptions( ArrayList<SubscriptionVO> listSubscriptionVO){
//		listSubscriptionVO = (ArrayList<SubscriptionVO>) listSubscriptionVO;
		 SubscriptionDAO sd = new SubscriptionDAO();
		 ArrayList<ChannelFeatureVO> featureVoList = null;
				 for (int i =0; i<listSubscriptionVO.size();i++){
					 
					 if(listSubscriptionVO.get(i) instanceof CableTvVO ){
						 cableTvVO = (CableTvVO) listSubscriptionVO.get(i);
						 cableTvVO.setSubsId("SUBS-"+sg.edu.nus.iss.billing.common.Util.generateRandomNumer());
						 cableTvVO.setAccNo(this.accountNo);
						 cableTvVO.setStartDate(sg.edu.nus.iss.billing.common.Util.getSystemDate());
						 cableTvVO.setEndDate(sg.edu.nus.iss.billing.common.Util.getDefaultEndDate());
						 
						 
						 
						 if( cableTvVO.getAllAddonChannel() !=null){
											 featureVoList = cableTvVO.getAllAddonChannel();
											 for (int j=0; j<featureVoList.size(); j++) {					
												 featureVoList.get(j).setStartDate(sg.edu.nus.iss.billing.common.Util.getSystemDate());
												 featureVoList.get(j).setEndDate(sg.edu.nus.iss.billing.common.Util.getDefaultEndDate());
//												 featureVoList.get(j).setSubscriptionRate(subscriptionRate)
												 
											}
						 }
						 
						 sd.createSubscription(cableTvVO);
					 }
					 else if(listSubscriptionVO.get(i) instanceof DigitalVoiceVO ){
						digitalVoiceVO = (DigitalVoiceVO)  listSubscriptionVO.get(i);
						setNewVoiceVODetails(digitalVoiceVO);
						
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+ digitalVoiceVO.getAccNo());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getSubsId());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getPhoneNumber());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getStatus());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getStartDate());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getEndDate());		
						
						 sd.createSubscription(digitalVoiceVO);
					 }	 else if(listSubscriptionVO.get(i) instanceof DigitalVoiceVO ){
						 mobileVoiceVO = (MobileVoiceVO)  listSubscriptionVO.get(i);
						 setNewVoiceVODetails(mobileVoiceVO);
						 
							System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+ mobileVoiceVO.getAccNo());
							System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getSubsId());
							System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getPhoneNumber());
							System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getStatus());
							System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getStartDate());
							System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getEndDate());	
							
						sd.createSubscription(mobileVoiceVO);
						 }
			 
		 }
	}
	
	private void setNewVoiceVODetails(VoiceVO voiceVO){
		ArrayList<FeatureVO> featureVoList  = null;

		
		voiceVO.setAccNo(this.accountNo);
		 if(voiceVO  instanceof MobileVoiceVO)		{
			if(voiceVO.getSubsId() == null ||   !mobileVoiceExists ) //if subs id is not null - reused the same like a new customer
					voiceVO.setSubsId("SUBS-"+sg.edu.nus.iss.billing.common.Util.generateRandomNumer());
			
		
		if((voiceVO.getSubsId() == null && voiceVO.getPhoneNumber() == null )|| !mobileVoiceExists )
				voiceVO.setPhoneNumber(sg.edu.nus.iss.billing.common.Util.generateRandomPhoneNumber());
		
		if((voiceVO.getSubsId() == null && voiceVO.getStartDate() == null) || !mobileVoiceExists)
			voiceVO.setStartDate(sg.edu.nus.iss.billing.common.Util.getSystemDate());
	}
		 
		 if(voiceVO  instanceof DigitalVoiceVO)		{
			if(voiceVO.getSubsId() == null ||   !digitalVoiceExists ) //if subs id is not null - reused the same like a new customer
					voiceVO.setSubsId("SUBS-"+sg.edu.nus.iss.billing.common.Util.generateRandomNumer());
			
		
		if((voiceVO.getSubsId() == null && voiceVO.getPhoneNumber() == null )||!digitalVoiceExists )
				voiceVO.setPhoneNumber(sg.edu.nus.iss.billing.common.Util.generateRandomPhoneNumber());
		
		if((voiceVO.getSubsId() == null && voiceVO.getStartDate() == null) || !digitalVoiceExists)
			voiceVO.setStartDate(sg.edu.nus.iss.billing.common.Util.getSystemDate());
	}
		
		voiceVO.setStatus(sg.edu.nus.iss.billing.common.Util.SUBSCRIPTION_STATUS_ACTIVE);
		
		
		voiceVO.setEndDate(sg.edu.nus.iss.billing.common.Util.getDefaultEndDate());
		
		
		 VoiceVO  planVoiceVO = null; 
		 
		 if(voiceVO  instanceof MobileVoiceVO){		
			 planVoiceVO = (MobileVoiceVO)hashPlanMap.get(Util.SUBSCRIPTIONVO_MOBILEVOICE);
			 if(planVoiceVO!=null) {
				 System.out.println(CLASSNAME + "setNewVoiceVODetails  MobileVoiceVO : planVoiceVO : "+planVoiceVO.getMonthlySuscription());	
			 voiceVO.setMonthlySuscription(planVoiceVO.getMonthlySuscription());
		 }
		 }
		
		 if(voiceVO  instanceof DigitalVoiceVO){		
			 planVoiceVO = (DigitalVoiceVO)hashPlanMap.get(Util.SUBSCRIPTIONVO_DIGITALVOICE);
			 if(planVoiceVO!=null) {
				 System.out.println(CLASSNAME + "setNewVoiceVODetails  DigitalVoiceVO : planVoiceVO : "+planVoiceVO.getMonthlySuscription());	
			 voiceVO.setMonthlySuscription(planVoiceVO.getMonthlySuscription());
		 }
		 }
		
		 if( voiceVO.getAllOptionalFeature() !=null){
			featureVoList = voiceVO.getAllOptionalFeature();
			 for (int j=0; j<featureVoList.size(); j++) {	
				 	if(voiceVO.getSubsId() == null && featureVoList.get(j).getStartDate () == null)
				 			featureVoList.get(j).setStartDate(sg.edu.nus.iss.billing.common.Util.getSystemDate());
				 	
				 featureVoList.get(j).setEndDate(sg.edu.nus.iss.billing.common.Util.getDefaultEndDate());
				 		
				 if(planVoiceVO!=null) {
				 		for(int k =0; k<planVoiceVO.getAllOptionalFeature().size();k++){
				 			System.out.println(CLASSNAME + " setNewVoiceVODetails : planVoiceVO : "+planVoiceVO.getAllOptionalFeature().get(k).getDescription());
				 				if(planVoiceVO.getAllOptionalFeature().get(k).getDescription().equalsIgnoreCase( featureVoList.get(j).getDescription())){
				 						
				 						System.out.println(CLASSNAME + " setNewVoiceVODetails : MATCH planVoiceVO : "+planVoiceVO.getAllOptionalFeature().get(k).getDescription());
				 						 
				 				}
				 			
					 
				 }
				 }
			}
}
	}
	
	private void setNewVCableTvDetails(CableTvVO cableTvVO){
		ArrayList<ChannelFeatureVO> featureVoList  = null;
		
		if(cableTvVO.getSubsId() == null ||  !cableTvExists)
				cableTvVO.setSubsId("SUBS-"+sg.edu.nus.iss.billing.common.Util.generateRandomNumer());
		 cableTvVO.setAccNo(this.accountNo);
		
		 if(cableTvVO.getSubsId() == null && cableTvVO.getStartDate() == null && !cableTvExists)
			 	cableTvVO.setStartDate(sg.edu.nus.iss.billing.common.Util.getSystemDate());
		 
		 cableTvVO.setEndDate(sg.edu.nus.iss.billing.common.Util.getDefaultEndDate());
		 cableTvVO.setStatus(sg.edu.nus.iss.billing.common.Util.SUBSCRIPTION_STATUS_ACTIVE);
		 CableTvVO planCableTvVO = (CableTvVO)hashPlanMap.get(Util.SUBSCRIPTIONVO_CABLETV);
		 
		 if(planCableTvVO!=null) {
			 cableTvVO.setMonthlySuscription(planCableTvVO.getMonthlySuscription());
		 }
		 

		 
		 if( cableTvVO.getAllAddonChannel() !=null){
							 featureVoList = cableTvVO.getAllAddonChannel();
							 for (int j=0; j<featureVoList.size(); j++) {		
									System.out.println(CLASSNAME + " setNewVCableTvDetails : cableTvVO : "+featureVoList.get(j).getDescription());
									
									 if(cableTvVO.getSubsId() == null && cableTvVO.getStartDate() == null)
										 	featureVoList.get(j).setStartDate(sg.edu.nus.iss.billing.common.Util.getSystemDate());
									 
								 featureVoList.get(j).setEndDate(sg.edu.nus.iss.billing.common.Util.getDefaultEndDate());
								 if(planCableTvVO!=null) {
									 for (int k=0; k<planCableTvVO.getAllAddonChannel().size(); k++){
										 			System.out.println(CLASSNAME + " setNewVCableTvDetails : planCableTvVO : "+planCableTvVO.getAllAddonChannel().get(k).getDescription());	
										 			if(planCableTvVO.getAllAddonChannel().get(k).getDescription().equalsIgnoreCase( featureVoList.get(j).getDescription())){
										 					featureVoList.get(j).setSubscriptionRate(planCableTvVO.getAllAddonChannel().get(k).getSubscriptionRate());
										 					System.out.println(CLASSNAME + " setNewVCableTvDetails :MATCH cableTvVO planCableTvVO : "+planCableTvVO.getAllAddonChannel().get(k).getSubscriptionRate());	
										 			}
										 
									 }
								 }
							}
		 }
		 
	}

	/* (non-Javadoc)
	 * @see sg.edu.nus.iss.billing.service.subscription.ISubscription#modifySubscription(java.util.List)
	 */
	@Override
	public void modifySubscription(List<SubscriptionVO> listSubscriptionVO) {
		 SubscriptionDAO sd = new SubscriptionDAO();
		boolean cableSetupDone = false;
		boolean mobileSetupDone = false;
		boolean digitalSetupDone = false;
		
	   List<SubscriptionVO> existListSubscription =  sd.getSubscriptionForAcct(accountNo);
	   checkIfSubscriptionExists(existListSubscription);
	   ArrayList<FeatureVO> featureVoList = null;
	   System.out.println(CLASSNAME + " modifySubscription : listSubscriptionVO size : "+listSubscriptionVO.size() );
	   
	   
		 for (int i =0; i<listSubscriptionVO.size();i++){
			  System.out.println(CLASSNAME + " modifySubscription : Inside for loop   : "+listSubscriptionVO.get(i) );
			  
			 if(listSubscriptionVO.get(i) instanceof CableTvVO){
				 cableSetupDone = true;
				 cableTvVO = (CableTvVO) listSubscriptionVO.get(i);
				 System.out.println(CLASSNAME + " modifySubscription : CableTvVO : cableTvExists: "+cableTvExists);
				 System.out.println(CLASSNAME + " modifySubscription : CableTvVO : cableTvExists: "+cableTvVO.getSubsId());
				 System.out.println(CLASSNAME + " modifySubscription : CableTvVO : cableTvExists: "+cableTvVO.getStartDate());
				 
				 
				 setNewVCableTvDetails(cableTvVO);
				 
				 
				 System.out.println(CLASSNAME + " modifySubscription : CableTvVO : cableTvExists: "+cableTvExists +" SubsId: "+ cableTvVO.getSubsId());
				 System.out.println(CLASSNAME + " modifySubscription : CableTvVO : cableTvExists: "+cableTvExists +" Acct No: "+ cableTvVO.getAccNo());
				 if(!this.cableTvExists ){
					 	sd.createSubscription(cableTvVO);
				 }else {
					 		try {
								sd.updateSubscription(cableTvVO);
							} catch (BadDaoCallException e) {
								e.printStackTrace();
							}
					 	}
//				 
//			 }else{
//				 
//				 if()
//				 	System.out.println(CLASSNAME + " modifySubscription CableTvVO : ELSE    : "+cableTvVO.getSubsId() );
//				 	sd.setSubscriptionStatus(cableTvVO.getSubsId(), Util.SUBSCRIPTION_STATUS_INACTIVE);
//			 }
			 }
			 	
			 	 if(listSubscriptionVO.get(i) instanceof DigitalVoiceVO){
			 		digitalVoiceVO = (DigitalVoiceVO) listSubscriptionVO.get(i);
			 		 System.out.println(CLASSNAME + " modifySubscription : DigitalVoiceVO : digitalVoiceExists: "+digitalVoiceExists);
			 		digitalSetupDone  = true;
				 	if(!this.digitalVoiceExists ){			 
					setNewVoiceVODetails(digitalVoiceVO);
						
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+ digitalVoiceVO.getAccNo());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getSubsId());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getPhoneNumber());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getStatus());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getStartDate());
						System.out.println(CLASSNAME + " createSubscriptions : digitalVoiceVO : "+digitalVoiceVO.getEndDate());		
						if(!this.digitalVoiceExists)
							sd.createSubscription(digitalVoiceVO);
						else
							try {
								sd.updateSubscription(digitalVoiceVO);
							} catch (BadDaoCallException e) {
								e.printStackTrace();
							}
					  }
			 	 }
			 	 
			 	 if(listSubscriptionVO.get(i) instanceof MobileVoiceVO){
			 		mobileVoiceVO = (MobileVoiceVO) listSubscriptionVO.get(i);
			 		 System.out.println(CLASSNAME + " modifySubscription : MobileVoiceVO : mobileVoiceExists: "+mobileVoiceExists);
			 		mobileSetupDone = true;
							 setNewVoiceVODetails(mobileVoiceVO);
							 
								System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+ mobileVoiceVO.getAccNo());
								System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getSubsId());
								System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getPhoneNumber());
								System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getStatus());
								System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getStartDate());
								System.out.println(CLASSNAME + " createSubscriptions : mobileVoiceVO : "+mobileVoiceVO.getEndDate());	
								
								if(!this.mobileVoiceExists)
									sd.createSubscription(mobileVoiceVO);
								else
									try {
										sd.updateSubscription(mobileVoiceVO);
									} catch (BadDaoCallException e) {
										e.printStackTrace();
									}
							 
						 
					
				 	 }
		 }
		 
		 //if cablesetup is not done, but cable subscription exists - then terminate cable subscription
		 setPlansAsMap((ArrayList<SubscriptionVO>) existListSubscription);
		 if(!cableSetupDone && cableTvExists ){
			 //System.out.println(CLASSNAME + " modifySubscription : CableTvVO : BECOMING INACTIVE  "+cableSetupDone+"    SubsId: "+ cableTvVO.getSubsId());
			 cableTvVO =  (CableTvVO) hashPlanMap.get(Util.SUBSCRIPTIONVO_CABLETV);
			 		System.out.println(CLASSNAME + " modifySubscription : CableTvVO : BECOMING INACTIVE    "+cableSetupDone+"    SubsId: "+ cableTvVO.getSubsId());
			 		sd.setSubscriptionStatus(cableTvVO.getSubsId(), Util.SUBSCRIPTION_STATUS_INACTIVE);
			 		System.out.println(CLASSNAME + " modifySubscription : CableTvVO : cableSetupDone  "+cableSetupDone+"    SubsId: "+ cableTvVO.getSubsId());
			 		 System.out.println(CLASSNAME + " modifySubscription : CableTvVO : cableSetupDone  "+cableSetupDone+" subsId: "+  cableTvVO.getSubsId());
			 
		 }
		 
		 if(!digitalSetupDone && digitalVoiceExists ){
			 digitalVoiceVO =  (DigitalVoiceVO) hashPlanMap.get(Util.SUBSCRIPTIONVO_DIGITALVOICE);
			 sd.setSubscriptionStatus(digitalVoiceVO.getSubsId(), Util.SUBSCRIPTION_STATUS_INACTIVE);
		 		System.out.println(CLASSNAME + " DigitalVoiceVO : DigitalVoiceVO : DigitalVoiceVO  "+digitalSetupDone+"    SubsId: "+ digitalVoiceVO.getSubsId());
		 		 System.out.println(CLASSNAME + " DigitalVoiceVO : DigitalVoiceVO : DigitalVoiceVO  "+digitalSetupDone+" subsId: "+  digitalVoiceVO.getSubsId());
			 
		 }
			 	
		 
		 if(!mobileSetupDone && mobileVoiceExists ){
			 mobileVoiceVO =  (MobileVoiceVO) hashPlanMap.get(Util.SUBSCRIPTIONVO_MOBILEVOICE);
			 sd.setSubscriptionStatus(mobileVoiceVO.getSubsId(), Util.SUBSCRIPTION_STATUS_INACTIVE);
			 
		 		System.out.println(CLASSNAME + " mobileVoiceExists : mobileVoiceExists : mobileVoiceExists  "+mobileSetupDone+"    SubsId: "+ mobileVoiceVO.getSubsId());
		 		 System.out.println(CLASSNAME + " mobileVoiceExists : mobileVoiceExists : mobileVoiceExists  "+mobileSetupDone+" subsId: "+  mobileVoiceVO.getSubsId());
			 
		 }
			 	
		 
	}

	private void  checkIfSubscriptionExists(List<SubscriptionVO> listSubscriptionVO) {
		List listofServices = new ArrayList();
		 for (int i =0; i<listSubscriptionVO.size();i++){
			 
			 if(listSubscriptionVO.get(i) instanceof CableTvVO){
				 this.cableTvExists = true;
			 }
			  if(listSubscriptionVO.get(i) instanceof MobileVoiceVO ){
				 this.mobileVoiceExists = true;
			 }	  if(listSubscriptionVO.get(i) instanceof DigitalVoiceVO ){
				 	this.digitalVoiceExists  = true;
				 }
	 
 
		 }
		
	}
	
	
	public double getThisMonthTotalCharges(AccountVO account)
	{	
		SubscriptionDAO subscriptionDAO = new SubscriptionDAO();
		double total= 0;
		//double GST = 0;
		//double total =0;
		List<SubscriptionVO> subscribedPlanList=  subscriptionDAO.getSubscriptionForAcct(account.getAccountNo());
		for(Iterator<SubscriptionVO> subscribed = subscribedPlanList.iterator(); subscribed.hasNext(); ) {
			//Subscription Main Plan
			 SubscriptionVO subscribedPlan = subscribed.next();
		
			 
	         double subscriptionCharges = 0;
	         double featureTotalCharges = 0;
	         
	         System.out.println("subscribedPlan::"+ subscribedPlan);
	         
	         //CableTV
	         if(subscribedPlan instanceof CableTvVO)
	         {
	        	 CableTvVO cableTVVO = (CableTvVO)subscribedPlan;
	        	 if(cableTVVO.getStatus().equals(Util.ACCOUNT_STATUS_ACTIVE))
	        	 {
	        		 subscriptionCharges =cableTVVO.getMonthlySuscription();
	        		 
	        		 //Mandatory Channels
	        		 /*List<StandardChannelFeatureVO> standardChannelList= cableTVVO.get3StandardChannels();
	        		 for(Iterator<StandardChannelFeatureVO> addonfeature = standardChannelList.iterator();addonfeature.hasNext();)
	        		 {
	        			 FeatureVO feature = addonfeature.next();
	        			 featureTotalCharges += getFeatureRate(feature, subscribedPlan.getSubsId(), Util.getSystemDate());
	        		 }*/
	        		 
	        		 List<ChannelFeatureVO> addonfeatureList= cableTVVO.getAllAddonChannel();
	        		 for(Iterator<ChannelFeatureVO> addonfeature = addonfeatureList.iterator();addonfeature.hasNext();)
	        		 {
	        			 FeatureVO feature = addonfeature.next();
	        			 featureTotalCharges += getFeatureRate(feature, subscribedPlan.getSubsId(), Util.getSystemDate());
	        			 System.out.println("Feature::"+ feature.getName()+"charges "+getFeatureRate(feature, subscribedPlan.getSubsId(), Util.getSystemDate()));
	        		 }
	        		 
	        	 }
	         }
	         //DigitalVoice
	         else if(subscribedPlan instanceof DigitalVoiceVO)
	         {
	        	 DigitalVoiceVO digitalVoiceVO = (DigitalVoiceVO)subscribedPlan;
	        	 if(digitalVoiceVO.getStatus().equals(Util.ACCOUNT_STATUS_ACTIVE))
	        	 {
	        		 subscriptionCharges =digitalVoiceVO.getMonthlySuscription();
	        		 
	        		 FeatureVO localCall = new LocalCallFeatureVO();
	        		 localCall = digitalVoiceVO.getLocalCallFeatureVO();
	        		 featureTotalCharges += getFeatureRate(localCall, subscribedPlan.getSubsId(), Util.getSystemDate());
	        		 
	        		 List<FeatureVO> addonfeatureList= digitalVoiceVO.getAllOptionalFeature();
	        		 for(Iterator<FeatureVO> addonfeature = addonfeatureList.iterator();addonfeature.hasNext();)
	        		 {
	        			 FeatureVO feature = addonfeature.next();
	        			 featureTotalCharges += getFeatureRate(feature, subscribedPlan.getSubsId(), Util.getSystemDate());
	        		 }
	        	 }
	         }
	         //MobileVoice
	         else if(subscribedPlan instanceof MobileVoiceVO)
	         {
	        	 MobileVoiceVO mobileVoiceVO = (MobileVoiceVO)subscribedPlan;
	        	 if(mobileVoiceVO.getStatus().equals(Util.ACCOUNT_STATUS_ACTIVE))
	        	 {
	        		 subscriptionCharges =mobileVoiceVO.getMonthlySuscription();
	        		 
	        		 FeatureVO localCall = new LocalCallFeatureVO();
	        		 localCall = mobileVoiceVO.getLocalCallFeatureVO();
	        		 featureTotalCharges += getFeatureRate(localCall, subscribedPlan.getSubsId(), Util.getSystemDate());
	        		 
	        		 List<FeatureVO> addonfeatureList= mobileVoiceVO.getAllOptionalFeature();
	        		 for(Iterator<FeatureVO> addonfeature = addonfeatureList.iterator();addonfeature.hasNext();)
	        		 {
	        			 FeatureVO feature = addonfeature.next();
	        			 featureTotalCharges += getFeatureRate(feature, subscribedPlan.getSubsId(), Util.getSystemDate());
	        		 }
	        	 }
	         }
	         total += subscriptionCharges + featureTotalCharges;
	         
	         
		}
		total += (total * 0.07);
		return total;
	}
	
	public double getFeatureRate(FeatureVO feature,String subscriptionId,Date date)
	{	

		Feature featuresvc= null;
		
        if(feature instanceof CallTransferFeatureVO)
        {
        	featuresvc = new CallTransferFeature();
        }

        else if(feature instanceof ChannelFeatureVO)
        {
        	featuresvc = new ChannelFeature();
        }
        else if(feature instanceof DataServiceFeatureVO)
        {
        	featuresvc = new DataServiceFeature();
        }
        else if(feature instanceof IDDCallFeatureVO)
        {
        	featuresvc = new IDDCallFeature();
        }
        else if(feature instanceof LocalCallFeatureVO)
        {
        	featuresvc = new LocalCallFeature();

        }
        else if(feature instanceof PremiumChannelFeatureVO)
        {
        	featuresvc = new PremiumChannelFeature();
        }
        else if(feature instanceof RoamingCallFeatureVO)
        {
        	featuresvc = new RoamingCallFeature();
        }
        else if(feature instanceof StandardChannelFeatureVO)
        {
        	featuresvc = new StandardChannelFeature();
        }
        if(feature != null)
        {
        	return featuresvc.computeCharges(feature, subscriptionId, date);
        }
       
        return 0;
	}

	@Override
	public List<SubscriptionVO> getAllSubscriptionsForAcct() {
		 SubscriptionDAO sd = new SubscriptionDAO();
		 List<SubscriptionVO> subslist =  sd.getSubscriptionForAcct(this.accountNo);
		return subslist;
	}
}
/*
ArrayList<FeatureVO> list = subscription.getAllOptionalFeature();
for (int i=0; i<list.size(); i++) {
	sb.append(delim);
	FeatureVO feat = list.get(i);
	sb.append(feat.getName());
	sb.append(delim);
	sb.append(feat.getStartDate().toString());
	sb.append(delim);
	sb.append(feat.getEndDate().toString());
}*/